package com.xuhai.blog19.common.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author PangJunjie
 * @Date 2023/2/6/006
 */
public class R extends HashMap<String, Object> {
    /*初始化状态值*/
    private static final Integer SUCCESS = 200;
    private static final Integer ERROR = 500;

    public R() {
        put("code", SUCCESS);
        put("msg", "success");
    }

    public static R ok() {
        return new R();
    }

    public static R ok(int code) {
        return ok(code, "success");
    }

    public static R ok(String msg) {
        return ok(SUCCESS, msg);
    }

    public static R ok(int code, String msg) {
        R resultJson = new R();
        resultJson.put("code", code);
        resultJson.put("msg", msg);
        return resultJson;
    }

    public static R ok(Map<String, Object> map) {
        R resultJson = new R();
        resultJson.putAll(map);
        return resultJson;
    }

    public static R error(int code, String msg) {
        R resultJson = new R();
        resultJson.put("code", code);
        resultJson.put("msg", msg);
        return resultJson;
    }

    public static R error(int code) {
        return error(code, "error");
    }

    public static R error(String msg) {
        return error(ERROR, msg);
    }

    public static R error() {
        return error(ERROR, "error");
    }

    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public R data(Object obj) {
        super.put("data", obj);
        return this;
    }

}
